package com.domino.server.facebook.chat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.util.Scanner;

import com.domino.server.user.WebContentUtil;
import com.domino.shared.Player;
import com.domino.shared.PlayerId;

public class ChatClient {
	/*
	 * 
	 <?xml version="1.0"?><stream:stream id="none" from="chat.facebook.com" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" version="1.0">
	 	<stream:error><xml-not-well-formed xmlns="urn:ietf:params:xml:ns:xmpp-streams"/></stream:error>
	 </stream:stream>
	 * 
	 */
	
	public static void main(String[] args) {
		ChatClient cc = new ChatClient();
		Player player = new Player(new PlayerId("ERER"), "Per Segersten", "token");
		cc.sendMessage2(player, "Hej");
	}
	
	private void sendMessage2(Player player, String string) {
		sout("Connecting to chat.facebook.com:5222");
		Socket connection = getConnection2("chat.facebook.com", 5222);
		sout("Connected");
		String msg = getPageContent2(connection);
		sout("Response: " + msg);
	}

	private String getPageContent2(Socket connection) {
		String result = null;
		try {
		    BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));

		    String str;
		    while ((str = rd.readLine()) != null) {
		    	if (result==null) {
		    		result = str;
		    	} else {
		    		result += str;
		    	}
		    }
		    rd.close();
		}  catch (IOException e) {
			throw new RuntimeException(e);
		}
	    
	    return result;

	}

	private Socket getConnection2(String host, int port) {
		try {
		    InetAddress addr = InetAddress.getByName(host);

		    // This constructor will block until the connection succeeds
		    Socket socket = new Socket(addr, port);
		    return socket;
		} catch (UnknownHostException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public void sendMessage(Player player, String message) {
		
		sout("Connecting to chat.facebook.com:5222");
		URLConnection connection = getConnection("xmpp://chat.facebook.com:5222");
	
		String msg = getPageContent(connection);
		sout("Response: " + msg);
		
		String mechanism = "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='X-FACEBOOK-PLATFORM' />";
		sout("Sending mechanism: " + mechanism);
		sendContent(connection, mechanism);
		
		msg = getPageContent(connection);
		sout("Response: " + msg);

	}
	
	private void sendContent(URLConnection connection, String content) {
		try {
			connection.setDoOutput(true);
			OutputStream os = connection.getOutputStream();
			os.write(content.getBytes());
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	private URLConnection getConnection(String url) {
		try {
			  URL fURL = new URL(url);
		      URLConnection connection = fURL.openConnection();
		      return connection;
		    } catch (IOException e) {
				throw new RuntimeException(e);
			}
	}

	private void sout(String msg) {
		System.out.println(msg);
	}

	private static final String END_OF_INPUT = "\\Z";
	public static String getPageContent(URLConnection connection) {
	    String result = null;
	    
		try {
	      Scanner scanner = new Scanner(connection.getInputStream());
	      scanner.useDelimiter(END_OF_INPUT);
	      result = scanner.next();
	    } catch (IOException e) {
			throw new RuntimeException(e);
		}
	    
	    return result;
	  }

}
